*The file replicates all the tables and figures in the manuscript  "Open Space in U.S. Urban Areas: Where Might There Be Too Much or Too Little of a Good Thing?", including the following three parts


*Table1,2,4,5; Figure1-7 ,Appendix Table B1, ,Figures tables and figures in Appendix D

cd "D:\dataset and code"

use "landuse_table.dta",clear				  
 
/***************************************************************************
*Figure1: share of natural open space 2011： variable: naturalos
***************************************************************************/
sum naturalos

/***************************************************************************
*Figure2: share of preserved open space 2011: preservedos
***************************************************************************/
sum preservedos
/***************************************************************************
*Figure3: share of developable open space 2011: unpreservedos
***************************************************************************/
sum preservedos
/***************************************************************************
*Figure4: share of all open space 2011: openspace
***************************************************************************/
sum openspace

use "C:\Users\Jialing Yu\Desktop\dataset and code\landuse_table.dta",clear

*use landuse_table.dta,clear
/***************************************************************************
*Table1. Shares of Natural, Preserved, and Developable Open Space 
in U.S. Metropolitan Statistical area. 2011
***************************************************************************/
estpost sum naturalos slopeabove15 watersurface wetlands preservedos preservedos_pub preservedos_pvt unpreservedos unpreservedos_pub unpreservedos_pvt developedland if year==2011


/***************************************************************************
*Table 2. Open Space in U.S. Metropolitan Statistical Areas
 with a Population of at Least One Million
***************************************************************************/
sum  pop2011 naturalos preservedos  unpreservedos if  pop2011!=.


/***************************************************************************
TableB1.Summary Statistics for Land Cover in the 345 
US Metropolitan Statistical Areas, in Square Kilometers
***************************************************************************/
 estpost sum water_sqkm opensp_sqkm developed_sqkm barren_sqkm forest_sqkm shrub_sqkm grass_sqkm agland_sqkm wetland_sqkm if year==2001
 estpost sum water_sqkm opensp_sqkm developed_sqkm barren_sqkm forest_sqkm shrub_sqkm grass_sqkm agland_sqkm wetland_sqkm if year==2006
 estpost sum water_sqkm opensp_sqkm developed_sqkm barren_sqkm forest_sqkm shrub_sqkm grass_sqkm agland_sqkm wetland_sqkm if year==2011
 
 
/***************************************************************************
*Figure1 B1 Change in the Share of Preserved Open Space between 
2001 and 2011 in U.S. Metropolitan Statistical Areas: dopenspace
***************************************************************************/

/****************************************************************
               Table 4-5;Appendix Table D2
****************************************************************/
**Bootstrap simulation of elasticities based on Model (D) in Table3
use final70_10,clear
global sincl_exog "midwest south west"
global endogvar "dlgtotunit  int_pop   int_lambdapop   opensp1 opensp2"
global excl_exog  "dprot_iv          ntrad_iv         dlgconstr_wage  dlgrsmcost   oscomm00 oscomm10  dlgsm1ls15_m3f dlgsm1outlin   dlgsm2ls15_m3f dlgsm2outlin "
global incl_exog "dlgmunipc dlgmedfminc dlgcommute bartik_emp lgjansunhrs immshock  lambda coast midwest south west "
gmm (eq1: dlgmedval - {xb: int_lambda int_lambdapop int_wri $sincl_exog } - {b0} )  (eq2: dlgmedval - {xc: $endogvar  $incl_exog  } - {c0} ), instruments(eq1: bartik_iv lgjansunhrs immshock prot_iv ntrad_iv iv1-iv6  $sincl_exog ,nocons)  instruments(eq2: $excl_exog $incl_exog ,nocons ) winitial(identity) wmatrix(robust)   

matrix b_sls = e(b)
matrix se_sls = e(V)

set more off
mat li b_sls
mat li se_sls
/*********************************************elasticities***************************************************************/

rename lgpop_base lgpop70
rename lgwri lgwri3w
keep geoid_cbsa lgpop* lambda lgwri3w osm   dlgmunipc  

gen lgpop70i = .
gen lambdai = .
gen lgwri3wi = .
gen osi = .
gen muni = .

gen selas = .
gen delas = .
gen dincelas = . 
gen doselas = .
gen dmunielas = .
gen dnoselas = .
gen osstar = .
gen mtlv = .
gen vstar = .

global newvar "selas delas dincelas doselas dmunielas dnoselas osstar mtlv vstar"
foreach x in $newvar {
gen  `x'_m = .
gen  `x'_se = .
gen  `x'_cil = .
gen  `x'_ciu = .
gen  `x'_z = .
gen  `x'_star = .
}
drop $newvar

*set seed 711

capture program drop test_optimals
program define test_optimals, rclass

**v1
drawnorm rand1-rand24, means(b_sls) cov(se_sls)
scalar intlambda = rand1[1]
scalar intlambdapop=rand2[1]
scalar intwri=rand3[1]
scalar totunit=rand8[1]
scalar intpop= 0
scalar dintlambda = rand10[1]
scalar hhinc=rand14[1]
scalar dlgos=rand11[1]
scalar dlgmunicoef=rand13[1]
scalar dlgnos=rand12[1]
drop rand1-rand24

set more off
global bootvar "lgpop70i lambdai lgwri3wi osi"

foreach x in $bootvar {
sum `x', meanonly 
scalar `x' = r(mean)
}


scalar selas = 1/(scalar(lambdai)*intlambda+scalar(lambdai)*scalar(lgpop70i)*intlambdapop + scalar(lgwri3wi)*intwri) 
scalar delas = 1/(totunit+scalar(lgpop70i)*intpop + scalar(lgpop70i)*scalar(lambdai)*dintlambda)
scalar dincelas = -delas*hhinc
scalar doselas = -delas*dlgos
scalar dmunielas = -delas*dlgmunicoef
scalar dnoselas = -selas*dlgnos
scalar diff = 0.32*(selas - delas)
scalar osstar = doselas/(diff-1)
scalar mtlv = scalar(osi) + doselas - scalar(osi)*diff 
scalar vstar = (scalar(osi) *(diff-1)-doselas)/diff

return scalar selas = scalar(selas)
return scalar delas = scalar(delas)
return scalar dincelas = scalar(dincelas)
return scalar doselas = scalar(doselas)
return scalar dmunielas = scalar(dmunielas)
return scalar dnoselas = scalar(dnoselas)
return scalar osstar = scalar(osstar)
return scalar mtlv = scalar(mtlv)
return scalar vstar = scalar(vstar)
return scalar diff = scalar(diff)
exit 
end

set more off


timer clear
local N = 1
local N = _N
timer on 1
local i = 1
while `i'<346 {
qui replace lambdai = lambda[`i']
qui replace lgpop70i = lgpop70[`i']
qui replace lgwri3wi = lgwri3w[`i']
qui replace osi = osm[`i']


qui mi erase test_optimals
#delimit ;
qui bs selas=r(selas) delas=r(delas) dincelas=r(dincelas)
doselas=r(doselas) dmunielas=r(dmunielas) dnoselas=r(dnoselas)  osstar=r(osstar) 
mtlv = r(mtlv) vstar = r(vstar) diff=r(diff), reps(1000) bca  : test_optimals;
#delimit cr


matrix temp_b = e(b)
matrix temp_se = e(se)
matrix temp_ci =e(ci_bca)


global newvar "selas delas dincelas doselas dmunielas dnoselas osstar mtlv vstar"
local j = 1
foreach x in $newvar {
qui replace `x'_m = temp_b[1,`j'] in `i'
qui replace `x'_se = temp_se[1,`j'] in `i'
qui replace `x'_cil = temp_ci[1,`j'] in `i'
qui replace `x'_ciu = temp_ci[2,`j'] in `i'
qui replace `x'_z = 1 if `x'_m <0 & `x'_ciu <0 in `i'
qui replace `x'_z = 1 if `x'_m >0 & `x'_cil >0 in `i'

local j = `j'+1
}

if (temp_b[1,2]<0 & temp_b[1,2]>-10 & temp_b[1,1]>0 ) {
local i = `i'+1
}
else{  
local i = `i'   
}
di temp_b[1,2]
di `i'
}

timer off 1

timer list

save elasticities.dta

use elasticities.dta,clear

/***************************************************************************
*Table 4.Summary Statistics of Housing Supply and Demand Elasticity 
Estimates in U.S. Metropolitan Statistical Areas:
***************************************************************************/
*percentage of MSAs
sum elasticity_supply elasticity_demand_price elasticity_demand_inc elasticity_demand_opensp elasticity_demand_muniexp
*percentage of MSAs with significant parameter estimates
sum elasticity_supply_sig elasticity_demand_price_sig elasticity_demand_inc_sig elasticity_demand_opensp_sig elasticity_demand_muniexp_sig

/***************************************************************************
*Table 5. Shares of U.S. Metropolitan Statistical Areas
 with Too Much or Too Little Open Space： variable vstar
***************************************************************************/
*percentage of MSAs
tab category  //1. too little open space;2. uncertain results; 3.likely too  much open space; 4. too much open space
*percentage of MSAs with significant parameter estimates
tab categorymsasig //1. too little open space;2. uncertain results; 3.likely too  much open space; 4. too much open space

/***************************************************************************
*Figure5: supply elasticity: elasticity_supply
***************************************************************************/
sum elasticity_supply

/***************************************************************************
*Figure6: demand elasticity: elasticity_demand_price
***************************************************************************/
sum elasticity_demand_price

/***************************************************************************
*Figure7: income elasticity: elasticity_demand_inc
***************************************************************************/
sum elasticity_demand_inc


/***************************************************************************
*Table D2. Estimates of Housing Demand and Supply Elasticities 
(Based on Model (d) in Table 3) is generated by the simulated results
***************************************************************************/
sum geoid_cbsa elasticity_supply elasticity_demand_price elasticity_demand_inc elasticity_demand_opensp elasticity_demand_muniexp vstar 


 









